var express = require('express')
var connection = require('../db/db_utill');

var router = express.Router()

// 用户点赞
router.put('/agree_topic', function (req, res) {
  //获取post数据
  const body = req.body
  const inserts = [body.user_id, body.topic_id]

  connection.query(`SELECT * FROM agree_topic WHERE user_id=? and topic_id = ?`, inserts, function (error, results, fields) {
    if (error) {
      throw error;
    } else if (results.length) {
      //用户已经点赞，则取消点赞
      connection.query(`DELETE FROM agree_topic WHERE user_id=? and topic_id=?`, inserts, function (error, results, fields) {
        if (error) {
          throw error;
        } else {
          // 更新点赞数据
          const topic_agreeNum = body.topic_agreeNum - 1
          const topicInfo = [topic_agreeNum, body.topic_id]
          connection.query(`UPDATE topic_info SET topic_agreeNum=? WHERE topic_id=?`, topicInfo, function (error, results, fields) {
            if (error) {
              throw error;
            } else {
              return res.json({
                status: 201,
                message: '取消点赞成功！'
              })
            }
          });
        }
      });
    } else {
      // 用户没有点赞
      connection.query(`INSERT INTO agree_topic value(?,?)`, inserts, function (error, results, fields) {
        if (error) {
          throw error;
        } else {
          // 更新点赞数据
          const topic_agreeNum = body.topic_agreeNum + 1
          const topicInfo = [topic_agreeNum, body.topic_id]
          connection.query(`UPDATE topic_info SET topic_agreeNum=? WHERE topic_id=?`, topicInfo, function (error, results, fields) {
            if (error) {
              throw error;
            } else {
              return res.json({
                status: 200,
                message: '点赞成功！'
              })
            }
          });
        }
      });
    }
  });
})

//获取用户点赞的话题
router.get('/agree_topic', function (req, res) {
  //获取url发送用户id
  var id = req.query.id
  connection.query(`SELECT topic_id FROM agree_topic WHERE user_id=? `, [id], function (error, results, fields) {
    if (error) {
      throw error;
    } else if (results) {
      const string = JSON.stringify(results);
      const jsons = JSON.parse(string);
      res.json(jsons)
    }
  });
})

//获取用户收藏的话题
router.get('/favorite_topic', function (req, res) {
  //获取url发送用户id
  var id = req.query.id
  connection.query(`SELECT topic_id FROM favorite_topic WHERE user_id=? `, [id], function (error, results, fields) {
    if (error) {
      throw error;
    } else if (results) {
      const string = JSON.stringify(results);
      const jsons = JSON.parse(string);
      res.json(jsons)
    }
  });
})

// 用户收藏
router.put('/favorite_topic', function (req, res) {
  //获取post数据
  const body = req.body
  const inserts = [body.user_id, body.topic_id]
  connection.query(`SELECT * FROM favorite_topic WHERE user_id=? and topic_id = ?`, inserts, function (error, results, fields) {
    if (error) {
      throw error;
    } else if (results.length) {
      //用户已经收藏，则取消收藏
      connection.query(`DELETE FROM favorite_topic WHERE user_id=? and topic_id=?`, inserts, function (error, results, fields) {
        if (error) {
          throw error;
        } else {
          return res.json({
            status: 201,
            message: '取消收藏成功！'
          })
        }
      });
    } else {
      // 用户没有收藏
      connection.query(`INSERT INTO favorite_topic value(?,?)`, inserts, function (error, results, fields) {
        if (error) {
          throw error;
        } else {
          return res.json({
            status: 200,
            message: '收藏成功！'
          })
        }
      });
    }
  });
})

module.exports = router;
